package com.qjc.demo.algorithm.两个有序数组合并为一个有序数组;

/**
 * @ClassName: Solution
 * @Author: qjc
 * @Date: 2023/8/30 09:39
 */
public class Solution {

    public static void main(String[] args) {
        int[] a = {1, 3, 5, 7, 9, 10, 11, 12};
        int[] b = {2, 4, 6, 8};
        int[] resultNums = new int[a.length + b.length];
        int i = 0, j = 0, k = 0;
        while (i < a.length && j < b.length) {
            if (a[i] < b[j]) {
                resultNums[k++] = a[i++];
            } else if (a[i] == b[j]) {
                resultNums[k++] = a[i];
                i++;
                j++;
            } else {
                resultNums[k++] = b[j++];
            }
        }
        while (i < a.length) {
            resultNums[k++] = a[i++];
        }
        while (j < b.length) {
            resultNums[k++] = b[j++];
        }
        for (int num : resultNums) {
            System.out.println(num);
        }
    }

}
